using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class WinPanel : BasePanel
{
    private Transform stars;
    private Transform starsImage;
    private Button backButton;
    private Button replayButton;
    private Button nextButton;

    public override void OnInit()
    {
        skinPath = "WinPanel";
        layer = PanelManager.Layer.Panel;
    }

    public override void OnShow()
    {
        stars = skin.transform.Find("Stars");
        starsImage = skin.transform.Find("StarImage");
        backButton = skin.transform.Find("MenuButton").GetComponent<Button>();
        replayButton = skin.transform.Find("ReplayButton").GetComponent<Button>();
        nextButton = skin.transform.Find("NextButton").GetComponent<Button>();

        ShowStars();
        backButton.onClick.AddListener(OnBackClick);
        replayButton.onClick.AddListener(OnReplayButton);
        nextButton.onClick.AddListener(OnNextClick);
    }

    public override void OnClose() { }

    private void ShowStars()
    {
        for (int i = 0; i < GameManager.instance.StarCount; i++)
        {
            GameObject go = Instantiate(starsImage.gameObject, stars);
            go.gameObject.SetActive(true);
        }
    }

    private void OnBackClick()
    {
        Close();
        SceneManager.LoadScene(1);
    }

    private void OnReplayButton()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        Close();
    }

    private void OnNextClick()
    {
        if (SceneManager.GetActiveScene().buildIndex + 1 < SceneManager.sceneCountInBuildSettings)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
            Close();
        }
    }
}
